package com.xiaobing.common.exception;

import com.xiaobing.common.res.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常拦截
 */

@RestControllerAdvice
@Slf4j
public class GlobalException {


    @ExceptionHandler(Exception.class)
    public <T> Result<T> exceptionHandler(Exception e) {
        if (e instanceof ServiceException) {
            ServiceException appException = (ServiceException) e;
            log.info("====================== 自定义异常 =============================: {}", appException.getMessage());
            return Result.error(appException.getCode(), appException.getMsg());
        }
        log.info("====================== 全局异常 =============================:{}", e.getMessage());
        return Result.error(500, "服务器端异常");
    }


}
